<script setup lang="ts">
import { ref } from 'vue';
import axios from 'axios';
import {useUserStore} from "@/store/useUserStore";
import {useRouter} from "vue-router";
import {ErrorUtils} from "@/utils/ErrorUtils";
// 从环境变量获取 Strapi API 基础 URL
const store = useUserStore();
// 定义表单数据
const form = ref({
  email: '',
  password: ''
});
// 定义错误信息
const router = useRouter();
// 登录函数
const handleLogin = async () => {
  store.login(form.value.email, form.value.password)
      .then(() => {
        router.push({ name: 'home' })
      })
      .catch((error: any) => {
        ErrorUtils.handleError(error)
      })
};
</script>

<template>
  <div class="login-container">
    <!-- 添加装饰元素 -->
    <div class="decorative-element top-left"></div>
    <div class="decorative-element bottom-right"></div>
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <span>用户登录</span>
        </div>
      </template>
      <el-form :model="form" @submit.prevent="handleLogin">
        <el-form-item label="邮箱">
          <el-input
              v-model="form.email"
              type="text"
              placeholder="请输入邮箱"
              clearable
          />
        </el-form-item>
        <el-form-item label="密码">
          <el-input
              v-model="form.password"
              type="password"
              placeholder="请输入密码"
              show-password
              clearable
          />
        </el-form-item>
        <el-form-item >
          <div style="text-align: center;width: 100%;">
            <el-button type="primary" native-type="submit" style="width: 150px;" >登录</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  /* 使用渐变背景 */
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

.login-card {
  width: 400px;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  z-index: 1;
}

.card-header {
  text-align: center;
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

/* 装饰元素样式 */
.decorative-element {
  position: absolute;
  width: 400px;
  height: 400px;
  border-radius: 50%;
  opacity: 0.3;
}

.top-left {
  top: -150px;
  left: -150px;
  background: #ffffff;
}

.bottom-right {
  bottom: -150px;
  right: -150px;
  background: #4e73df;
}
</style>
